package com.fantasy.dott;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Reader;

import com.fantasy.file.GetFolderAbsPath;
import com.fantasy.file.ReadTxt;
import com.fantasy.file.WriteTxt;

public class GetDotT {

	private static String autoFolder = "autoEmpty";
	
	public static void get(String path) {
		Reader r = null;
		try {
			r = ReadTxt.GetFileReader(path);
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		BufferedReader bufReader = new BufferedReader(r);
		
		String templatePath = GetFolderAbsPath.getFolderPath("trigger");
		String delTemplate = getTemplate(templatePath + "xxdeld.t");
		String wriTemplate = getTemplate(templatePath + "xxwriw.t");
		try {
			String tableName = bufReader.readLine();
			String tempString = "";
			while(null != tableName){
				
				int index = tableName.indexOf("_");
				if(index == -1){
					index = tableName.length();
				}
				if(index != -1){
					String sortName = tableName.substring(0 , index);
					if("equipment".equals(sortName)){
						sortName = "eq";
					}
					if("transfer".equals(sortName)){
						sortName = "eqtr";
					}
					if("maintenance".equals(sortName)){
						sortName = "eqmt";
					}
					WriteTxt.open(templatePath + autoFolder + "/xx" + sortName + "d.t" );
					tempString = delTemplate.replace("#@#", tableName).replace("#@U#", tableName.toUpperCase()).replace("#@-#", sortName);
					File trueFile = new File(templatePath + "triggers/" + sortName + "d.t");
					//System.out.println("*****" + trueFile.getAbsolutePath());
					if(trueFile.exists()){
						System.out.println(tableName + "已存在");
						tempString = tempString.replace("TRIGGER PROCEDURE FOR DELETE OF " + tableName.toUpperCase() + ".", "{" + sortName + "d.t}");
						String real = ReadTxt.read(templatePath + "Orig/" + sortName + "d.t");
						if(real.indexOf("mfdeclre.i") != -1){
							System.out.println("有     *--*------*-" + sortName + "d.t");
							tempString = tempString.replace("{mfdeclre.i}", "");
						}
						WriteTxt.write(tempString);
					}else{
						WriteTxt.write(tempString);
					}
					WriteTxt.close();
					WriteTxt.open(templatePath + autoFolder + "/xx" + sortName + "w.t" );
					tempString = wriTemplate.replace("#@#", tableName).replace("#@U#", tableName.toUpperCase()).replace("#@-#", sortName);
					trueFile = new File(templatePath + "triggers/" + sortName + "w.t");
					if(trueFile.exists()){
						System.out.println(tableName + "已存在");
						String real = ReadTxt.read(templatePath + "Orig/" + sortName + "w.t");
						if(real.indexOf("mfdeclre.i") != -1){
							System.out.println("有     *--*------*-" + sortName + "w.t");
							tempString = tempString.replace("{mfdeclre.i}", "");
						}
						WriteTxt.write(tempString);
					}else{
						tempString = tempString.replace("{" + sortName + "w.t}" , "TRIGGER PROCEDURE FOR WRITE OF " + tableName.toUpperCase() + " OLD BUFFER OLD_" + tableName.toUpperCase() + ".");
						WriteTxt.write(tempString);
					}
					WriteTxt.close();
				}
				tableName = bufReader.readLine();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static String getTemplate(String path){
		return ReadTxt.read(path);
	}
	
	public static void main(String[] args) {
		String path = GetFolderAbsPath.getFolderPath("trigger") + "test.txt";
		System.out.println("************");
		get(path);
	}
}
